const express = require("express");
const router = express.Router();
const Response = require("../utils/Response.js");
const fs  = require('fs')

// 引入mysql连接池
const pool = require("../utils/db.js");

// 首页运功类型
// 新增运动

router.post("/sports/add", (req, res, next) => {
    let obj = req.body;
    pool.query("insert into sports_type set ?", [obj], (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      if(result.affectedRows===0){
        res.send({code:201,msg:'新增失败'})
      }else{
        res.send({ code: 200, msg: '新增成功' })
      }
    });
  });

// 查询所有运动类型
router.get("/sports", (req, res, next) => {
    let obj = req.query;
    console.log(obj);
    pool.query("select * from sports_type", [obj], (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      //结果是对象，如果对象下的属性length为0说明修改失败
      if (result.length === 0) {
        res.send({ code: 201, msg: "查询失败" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    });
  });

  
// 删除运动类型
router.post('/sports/delete', (req, res, next) => {
	let obj = req.body;
	console.log(obj);
		pool.query('delete from sports_type where id=?', [obj.id], (err, result) => {
			if (err) {
				return next(err);
			}
			else {
				res.send({ code: 200, msg: '删除成功' });
			}
	});
});

// 不同运动类型查询不同的课程，传参pid
router.get('/course',(req,res,next)=>{
	let obj=req.query;
  console.log(obj);
	pool.query('select course_project.* from sports_type,course_project where id = ? and id=sid;',[obj.id],(err,result)=>{
		if(err){
			return next(err);
		}
		console.log(result);
		//结果是对象，如果对象下的属性length为0说明修改失败
		if(result.length===0){
			res.send({code:201,msg:'查询失败'});
		}else{
			res.send({code:200,msg:'查询成功',data:result});
		}
	});
})


module.exports = router;
